<template>
  <view class="page-container">
    <GlobalTopBar ref="topBarRef" class="topbars" :isLeft="true">
      <template #title><text>优惠券</text></template>
    </GlobalTopBar>
    <view class="page_content">
      <!-- 记录 -->
      <view class="ulbox">
        <scroll-view
          class="scroll-Y"
          scroll-y
          refresher-enabled
          :refresher-triggered="isRefreshing"
          @refresherrefresh="handleRefresh"
          @scrolltolower="handleLoadMore"
          refresher-background="#f5f5f5"
          refresher-threshold="80"
        >
          <view
            v-for="(item, index) in listData"
            :key="index + 1"
            :class="[
              'libox',
              `a${item.coupon_type}`,
              item.status != 1 ? 'ac' : '',
            ]"
            @click="drawerRef.open(), (datas = item)"
          >
            <view class="left">
              <template v-if="item.coupon_type != 3">
                <text class="t1" v-if="item.coupon_type === 1"
                  ><text style="font-size: 24rpx">￥</text
                  >{{ item.reduce_price }}</text
                >
                <text class="t1" v-else
                  >{{ item.discount
                  }}<text style="font-size: 24rpx">折</text></text
                >
              </template>

              <view class="t3">{{ coupon_typeData[item.coupon_type] }}</view>
            </view>
            <view class="right">
              <view>
                <view class="t1">{{ item.coupon_title }}</view>
                <view class="t2"
                  >有效期：{{
                    `${formatTimestamp(
                      item.validity_start_at,
                    )}~${formatTimestamp(item.validity_end_at)}`
                  }}</view
                >
                <!-- <view class="t3">{{
                  item.is_superposition === 1 ? '可叠加' : '不可叠加'
                }}</view> -->
              </view>
              <view class="vb" v-if="item.is_superposition === 2"
                ><text>{{
                  item.is_superposition === 1 ? '可叠加' : '卡券详情'
                }}</text
                ><view
                  ><uni-icons
                    style="transform: translateY(0rpx)"
                    color="#fff"
                    type="down"
                    size="12"
                  ></uni-icons></view
              ></view>
            </view>
          </view>
        </scroll-view>
        <view v-if="!listData.length" style="text-align: center">
          <image
            class="wyhq_img"
            :src="`${store.imgDomain}coupon_1.png`"
            mode="widthFix"
          />
          <view class="wtext">您还没有优惠券哦~</view>
        </view>
      </view>
      <!-- 文字 -->
      <view class="btext">
        <image :src="`${store.imgDomain}my_text.png`" mode="scaleToFill" />
      </view>
    </view>

    <y-drawer
      ref="drawerRef"
      :borderRadius="20"
      :showMask="true"
      :maskClosable="true"
      :length="500"
    >
      <view class="couponD_box">
        <view class="topsbox flex_center">
          <image
            class="coupon_2_img"
            :src="`${store.imgDomain}coupon_2.jpg`"
            mode="scaleToFill"
          />
          <text>卡券详情</text>
        </view>
        <view class="tlbox">
          <view class="ulbox">
            <scroll-view
              style="
                height: 400rpx;
                box-sizing: border-box;
                padding-bottom: 40rpx;
              "
              scroll-y
            >
              <view class="titles">{{ datas.coupon_title }}</view>
              <view class="desc" v-html="datas.coupon_desc"></view>
            </scroll-view>
          </view>
        </view>
      </view>
    </y-drawer>
  </view>
</template>

<script setup>
import { ref, computed, reactive } from 'vue';
import { useCounterStore } from '@/store';
import { onLoad, onShow } from '@dcloudio/uni-app';
import request from '@/utils/request';
import { formatTimestamp } from '@/utils/validate';

const store = useCounterStore();
const userInfo = ref({});
const userAddressList = ref([]);
const isTrue = ref(false);
const isXq = ref(false);
// 列表数据相关
const listData = ref([]);
const pageNum = ref(1);
const pageSize = ref(10);
const isNext = ref(true);
// 状态控制
const isRefreshing = ref(false); // 下拉刷新状态
const isLoading = ref(false); // 加载更多状态
const datas = ref({});
const drawerRef = ref(null);
const coupon_typeData = {
  1: '代金券',
  2: '折扣券',
  3: '免上门费券',
};
// 获取会员优惠券列表数据(分页)
const getUserBalanceLogListByRechargeCard = async (isRefresh = false) => {
  try {
    const data = await request('wxapp/user/getUserCouponList', {
      currpage: pageNum.value,
      pagesize: pageSize.value,
    });
    isNext.value = data.page.isNext;
    if (isRefresh) {
      listData.value = [...data.list];
    } else {
      listData.value = [...listData.value, ...data.list];
    }
    listData.value = listData.value.map((item) => {
      item.coupon_desc = item.coupon_desc.replace(/\n/g, '<br>');
      return item;
    });
  } finally {
    isRefreshing.value = false;
    isLoading.value = false;
  }
};

// 下拉刷新处理
const handleRefresh = () => {
  // 重置状态
  pageNum.value = 1;
  isRefreshing.value = true;
  // 重新加载数据
  getUserBalanceLogListByRechargeCard(true);
};

// 触底加载更多处理
const handleLoadMore = () => {
  if (!isNext.value || isLoading.value) return;
  // 加载下一页
  pageNum.value++;
  isLoading.value = true;
  getUserBalanceLogListByRechargeCard();
};

onShow(() => {
  getUserBalanceLogListByRechargeCard(true);
});
</script>

<style lang="scss" scoped>
.topbars {
  background-color: #ffffff;
}
.topl {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 0;
  image {
    width: 750rpx;
    height: 364rpx;
  }
}
.page_content {
  box-sizing: border-box;
  padding: 20rpx;
  position: relative;

  .zzc {
    padding: 50rpx 0;
    text-align: center;
    position: relative;
    z-index: 1;
    border: 1px solid #437ef6;
    border-radius: 20rpx;
    background-color: #fff;
    margin-bottom: 30rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    .t1 {
      font-size: 30rpx;
      color: #1e1e1e;
    }
    .t2 {
      font-size: 60rpx;
      font-weight: 600;
      color: #1e1e1e;
    }
    .czbtn {
      width: 409rpx;
      line-height: 79rpx;
      border-radius: 40rpx;
      background-color: #ff6316;
      color: #fff;
      font-size: 36rpx;
      margin: 20rpx auto;
    }
  }
  .ulbox {
    .libox {
      position: relative;
      overflow: hidden;
      display: flex;
      height: 222rpx;
      border-radius: 20rpx;
      margin-bottom: 25rpx;
      font-size: 24rpx;
      &::after {
        content: ' ';
        position: absolute;
        top: -15rpx;
        left: 28.5%;
        width: 30rpx;
        height: 30rpx;
        border-radius: 50%;
        background-color: #eaf2fc;
      }
      &::before {
        content: ' ';
        position: absolute;
        bottom: -15rpx;
        left: 28.5%;
        width: 30rpx;
        height: 30rpx;
        border-radius: 50%;
        background-color: #eaf2fc;
      }
      &.a1 {
        background-color: #2f86f1;
        .left {
          .t3 {
            color: #2f86f1;
          }
        }
      }
      &.a2 {
        background-color: #ff5340;
        .left {
          .t3 {
            color: #ff5340;
          }
        }
      }
      &.a3 {
        background-color: #ff9600;
        .left {
          .t3 {
            color: #fff;
            font-size: 32rpx;
            background-color: #ff9600;
          }
        }
      }
      &.ac {
        filter: grayscale(1);
      }
      .left {
        width: 31%;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        color: #fff;
        .t1 {
          font-size: 42rpx;
        }
        .t2 {
          margin-bottom: 10rpx;
        }
        .t3 {
          line-height: 36rpx;
          padding: 0 13rpx;
          color: #2f86f1;
          background-color: #fff;
          border-radius: 10rpx;
        }
      }
      .right {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        box-sizing: border-box;
        padding: 20rpx;
        flex: 1;
        background-color: #fff;
        .t1 {
          font-size: 30rpx;
        }
        .t2 {
          margin: 10rpx 0;
          font-size: 24rpx;
          color: $uni-text-color-grey;
        }
        .t3 {
          font-size: 24rpx;
          color: $uni-text-color-grey;
        }
        .vb {
          display: flex;
          justify-content: space-between;
          color: $uni-text-color-grey;
          view {
            width: 30rpx;
            height: 30rpx;
            border-radius: 50%;
            background-color: #c3c3c3;
            display: flex;
            align-items: center;
            justify-content: center;
          }
        }
      }
    }
  }
}
.scroll-Y {
  max-height: 60vh;
}
.xqbox {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.7);
  .bbox {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    background-color: #fff;
    box-sizing: border-box;
    padding: 20rpx 20rpx 60rpx;
    font-size: 24rpx;
    .h1 {
      color: #000;
      font-size: 36rpx;
      font-weight: 700;
      margin-bottom: 20rpx;
    }
  }
}
.btext {
  position: relative;
  margin-top: 35rpx;
  display: flex;
  justify-content: center;
  &::after {
    content: ' ';
    width: 100rpx;
    height: 1rpx;
    position: absolute;
    top: 50%;
    left: 140rpx;
    transform: translateY(-50%);
    background-color: #73adff;
  }
  &::before {
    content: ' ';
    width: 100rpx;
    height: 1rpx;
    position: absolute;
    top: 50%;
    right: 140rpx;
    transform: translateY(-50%);
    background-color: #73adff;
  }
  image {
    position: relative;
    display: block;
    opacity: 0.6;
    width: 185rpx;
    height: 38rpx;
  }
}
.wyhq_img {
  display: block;
  margin: 200rpx auto 20rpx;
  width: 300rpx;
}
.wtext {
  text-align: center;
  color: $uni-text-color-grey;
}

.couponD_box {
  position: relative;
  color: #000;
  .topsbox {
    box-sizing: border-box;
    padding: 0 46rpx;
    position: relative;
    width: 750rpx;
    height: 109rpx;
    justify-content: flex-start;
    text {
      position: relative;
      z-index: 2;
      font-size: 36rpx;
      font-weight: 700;
    }
    .coupon_2_img {
      position: absolute;
      top: 0;
      left: 0;
      border-radius: 20rpx 20rpx 0 0;
      width: 750rpx;
      height: 109rpx;
    }
  }
  .tlbox {
    box-sizing: border-box;
    padding: 20rpx 46rpx;
    .titles {
      text-align: center;
      font-size: 36rpx;
      font-weight: 700;
      margin-bottom: 20rpx;
    }
    .desc {
      line-height: 40rpx;
    }
  }
}
</style>
